<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline='true'>
				<el-form-item label="添加角色"></el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-plus" @click="handleAdd()"></el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="roles" highlight-current-row v-loading="listLoading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="id" label="ID" width="150" sortable>
			</el-table-column>
			<el-table-column prop="name" label="名称" width="180" sortable>
			</el-table-column>
			<el-table-column prop="code" label="编码" width="180" sortable>
			</el-table-column>
			<el-table-column prop="status" label="状态" width="180" :formatter="formatStatus" sortable>
			</el-table-column>
			<el-table-column label="操作" width="330">
				<template slot-scope="scope">
					<el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)"></el-button>
					<el-button type="danger" icon="el-icon-delete" @click="handleDel(scope.$index, scope.row)"></el-button>
					<el-button type="success" icon="el-icon-success" @click="handleRes(scope.$index, scope.row)">授权资源</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange" :page-size="pagination.pageSize" :total="pagination.total" style="float:left;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
			<el-form-item label="名称" prop="name">
				<el-input v-model="editForm.name"></el-input>
			</el-form-item>
				<el-form-item label="编码">
					<el-input v-model="editForm.code"></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="name">
					<el-select v-model="editForm.status" clearable placeholder="请选择">
						<el-option
							v-for="item in statusOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="名称" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="编码" prop="code">
					<el-input v-model="addForm.code"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-select v-model="addForm.status" clearable placeholder="请选择">
						<el-option
							v-for="item in statusOptions2"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--授权资源界面-->
		<el-dialog title="已授权资源" :visible.sync="resTableVisible">
			<el-table :data="authRes" max-height="250" highlight-current-row v-loading="resLoading"  @selection-change="selsChange" style="width: 100%;">
				<el-table-column type="index" width="60">
				</el-table-column>
				<el-table-column prop="id" label="ID" width="100" sortable>
				</el-table-column>
				<el-table-column prop="name" label="名称" width="150" sortable>
				</el-table-column>
				<el-table-column prop="code" label="编码" width="150" sortable>
				</el-table-column>
				<el-table-column prop="url" label="URI" width="150" sortable>
				</el-table-column>
				<el-table-column prop="method" label="访问方式" width="150" sortable>
				</el-table-column>
				<el-table-column prop="status" label="状态" width="120" :formatter="formatStatus" sortable>
				</el-table-column>
				<el-table-column label="操作" width="150">
					<template slot-scope="scope2">
						<el-button type="danger" icon="el-icon-delete" @click="handleDelRes(scope2.$index, scope2.row)"></el-button>
					</template>
				</el-table-column>
			</el-table>
			<!--工具条-->
			<el-col :span="24" class="toolbar">
				<el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange2" :page-size="pagination2.pageSize" :total="pagination2.total" style="float:left;">
				</el-pagination>
			</el-col>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click.native="handleApi">添加</el-button>
				<el-button @click.native="resTableVisible = false">关闭</el-button>
			</div>
		</el-dialog>

		<!--未授权资源界面-->
		<el-dialog title="未授权资源" :visible.sync="apiTableVisible">
			<el-table :data="restApis" max-height="250" highlight-current-row v-loading="apiLoading" @selection-change="selsChange" style="width: 100%;">
				<el-table-column type="index" width="60">
				</el-table-column>
				<el-table-column prop="id" label="ID" width="100" sortable>
				</el-table-column>
				<el-table-column prop="name" label="名称" width="150" sortable>
				</el-table-column>
				<el-table-column prop="code" label="编码" width="150" sortable>
				</el-table-column>
				<el-table-column prop="url" label="URI" width="150" sortable>
				</el-table-column>
				<el-table-column prop="method" label="访问方式" width="150" sortable>
				</el-table-column>
				<el-table-column prop="status" label="状态" width="120" :formatter="formatStatus" sortable>
				</el-table-column>
				<el-table-column label="操作" width="150">
					<template slot-scope="scope3">
						<el-button type="danger" icon="el-icon-plus" @click="handleAuthApi(scope3.$index, scope3.row)"></el-button>
					</template>
				</el-table-column>
			</el-table>
			<!--工具条-->
			<el-col :span="24" class="toolbar">
				<el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange3" :page-size="pagination3.pageSize" :total="pagination3.total" style="float:left;">
				</el-pagination>
			</el-col>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="authSubmit">关闭</el-button>
				<el-button type="primary" @click.native="authSubmit">确定</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import { instance, getRoleListPage, addRole, removeRole, editRole, getAuthResPage, getRestApiPage, removeAuthRes, addAuthRes } from '../../api';

	export default {
		data() {
			return {
				roles: [],
				authRes: [],
				restApis: [],
				roleId: 0,
				pagination: {
					total: 0,
					pageNum: 1,
					pageSize: 20,
				},
				pagination2: {
					total: 0,
					pageNum: 1,
					pageSize: 10,
				},
				pagination3: {
					total: 0,
					pageNum: 1,
					pageSize: 10,
				},
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入角色名称', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					id: 0,
					name: '',
					code: '',
					status: 0
				},
				statusOptions: [{
          value: 1,
          label: '在线'
        }, {
          value: 9,
          label: '禁用'
        }],

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入角色名称', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					name: '',
					code: '',
					status: 0
				},
				statusOptions2: [{
          value: 1,
          label: '在线'
        }, {
          value: 9,
          label: '禁用'
        }],
				resTableVisible: false,
				resLoading: false,
				apiTableVisible: false,
				apiLoading: false,
			}
		},

		methods: {
			//状态显示转换
			formatStatus: function (row, column) {
				return row.status == 1 ? '正常' : row.status == 9 ? '禁用' : '未知';
			},
			handleCurrentChange(currentPage) {
				this.pagination.pageNum = currentPage;
				this.getRoles();
			},
			handleCurrentChange2(currentPage) {
				this.pagination2.pageNum = currentPage;
				this.getAuthRes();
			},
			handleCurrentChange3(currentPage) {
				this.pagination3.pageNum = currentPage;
				this.getRestApi();
			},
			//获取api列表
			getRoles() {
				let params = {
					currentPage: this.pagination.pageNum,
					pageSize: this.pagination.pageSize
				};
				console.log(params);
				this.listLoading = true;
				//NProgress.start();
				getRoleListPage(params).then((res) => {
					this.pagination.total = res.data.data.roles.total;
					this.roles = res.data.data.roles.list;
					this.listLoading = false;
					//NProgress.done();
				});
			},

			// 获取角色授权资源
			getAuthRes() {
				let params = {
					roleId: this.roleId,
					currentPage: this.pagination2.pageNum,
					pageSize: this.pagination2.pageSize
				};
				this.resLoading = true;
				getAuthResPage(params).then((res) => {
					if(res.data.code === 2000) {
						if(res.data.data.authRes.total > 0) {
							this.pagination2.total = res.data.data.authRes.total;
							this.authRes = res.data.data.authRes.list;
							this.resLoading = false;
							console.log(this.roleId);
						}else {
							this.authRes = [];
							this.resLoading = false;
							this.$message({
								message: '此角色没有授权资源！',
								type: 'warning'
							});
						}
					}else{
						this.authRes = [];
						this.resLoading = false;
						this.$message({
							message: '系统异常!',
							type: 'error'
						});
					}

				});
			},
			// 获取角色未授权资源
			getRestApi() {
				let params = {
					roleId: this.roleId,
					currentPage: this.pagination3.pageNum,
					pageSize: this.pagination3.pageSize
				};
				console.log(params);
				this.apiLoading = true;
				getRestApiPage(params).then((res) => {
					this.pagination3.total = res.data.data.restApis.total;
					this.restApis = res.data.data.restApis.list;
					this.apiLoading = false;
				});
			},

			// 加载角色授权资源界面
			handleRes: function(index, row) {
				this.roleId = row.id;
				this.resTableVisible = true;
				this.getAuthRes();
				console.log(this.roleId);
			},
			// 加载添加角色授权资源界面
			handleApi: function() {
				this.apiTableVisible = true;
				this.getRestApi();
			},
			authSubmit: function() {
				this.apiTableVisible = false;
				this.getAuthRes();
			},
			//删除已授权资源
			handleDelRes: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.resLoading = true;
					//NProgress.start();
					let params = { roleId: this.roleId, resourceId: row.id };
					console.log(params);
					removeAuthRes(params).then((res) => {
						this.resLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getAuthRes();
					});
				}).catch(() => {

				});
			},
			//添加授权资源
			handleAuthApi: function (index, row) {
				this.$confirm('确认添加该资源吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.apiLoading = true;
					//NProgress.start();
					let params = { roleId: this.roleId, resourceId: row.id };
					console.log(params);
					addAuthRes(params).then((res) => {
						this.apiLoading = false;
						//NProgress.done();
						this.$message({
							message: '添加成功',
							type: 'success'
						});
						this.getRestApi();
					});
				}).catch(() => {

				});
			},
			//删除角色
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let params = { roleId: row.id };
					removeRole(params).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getRoles();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					name: '',
					code: '',
					status: 0,
				};
			},
			//编辑角色
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let params = Object.assign({}, this.editForm);
							editRole(params).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getRoles();
							});
						});
					}
				});
			},
			//新增角色
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let params = Object.assign({}, this.addForm);
							addRole(params).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getRoles();
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let params = { ids: ids };
					batchRemoveUser(params).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getRoles();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getRoles();
		}
	}

</script>

<style scoped>

</style>
